﻿// Code0101.cpp : 整数数组的起泡排序
//包含带bool标志和不带bool标志

#include <iostream>
using namespace std;
//包含bool标志的起泡排序
//借助bool型sorted可方便提前退出，不致总是蛮力做n-1次扫描
void BubbleSort1(int intptr[], int n)
{
	bool sorted = false;
	int x = 0;
	while (!sorted)
	{

		sorted = true;
		for (int i = 1; i < n; ++i)
		{
			++x;
			if (intptr[i-1]>intptr[i])
			{
				
				swap(intptr[i - 1], intptr[i]);
				sorted = false;
			}
		}
		--n;

	}
	cout << x<<endl;
}
//不包含bool标志的起泡排序
void BubbleSort2(int* intptr, int n)
{
	int x = 0;
	for (int i = n; i>1; --i)
	{
		for (int j = 1; j < i; ++j)
		{
			++x;
			if (intptr[j-1]>intptr[j])
			{
				
				swap(intptr[j - 1], intptr[j]);
			}	
		}
	}
	cout << x << endl;
}
ostream& operator <<(ostream& out, const int* intptr)
{
	out << "数组为：{";
	for (int i = 0; i < 7; ++i)
	{
		out << intptr[i] << ",";
	}
	out << "\b}" << endl;
	return out;
}

int main()
{
	int a[] = { 5,2,7,4,6,3,1 };
	int b[]= { 5,2,7,4,6,3,1 };
	cout << a;
	cout << "排序后：" << endl;
	BubbleSort1(a, 7);
	cout << a;
	BubbleSort2(b, 7);
	cout << b;
	return 0;
}


